<?php
class EditUserCtl extends BaseCtl {
	public function EditUserCtl(){
		parent::BaseCtl();
	}

	public function submit(){
		try {
			$form = $this->getEditUserForm();
			$adding = $form->isAdding();
				
			$usersForm = $this->getUsersForm();
				
			$user = $form->getUser();
				
			$service = new Service();
				
			if($adding){
				$service->addUser($user);
				$usersForm->addUser($user);
			}
			else {
				$service->updateUser($user);
				$usersForm->updateUser($user);
			}
			
			// success? go back to users.php
			$this->redirect('users.php'); 
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}

	public function cancel(){
		try {
			$this->redirect('users.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}

	public function isPostback(){
		return 'edituser' == $this->getPost('pagename');
	}

	public function loadData(){
		$form = $this->getEditUserForm();
		 
		$form->setAdding('yes' == $this->getPost('adding'));
		$form->setAdmin('on' == $this->getPost('admin'));
		$form->setConnect('on' == $this->getPost('connect'));
		$form->setSendEmail('on' == $this->getPost('sendEmail'));

		$form->setId($this->getPost('id'));
		$form->setLogin($this->getPost('login'));
		$form->setName($this->getPost('name'));
		$form->setEmail($this->getPost('email'));
	}
}
?>